package com.zrwl.poscloud.xo.service.impl;
import com.zrwl.poscloud.base.entity.TimeTableDetail;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrwl.poscloud.base.enums.EStatus;
import com.zrwl.poscloud.base.superI.SuperServiceImpl;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.utils.StringUtils;
import com.zrwl.poscloud.xo.global.MessageConf;
import com.zrwl.poscloud.xo.global.SQLConf;
import com.zrwl.poscloud.xo.global.SysConf;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import com.zrwl.poscloud.xo.mapper.TimeTableDetailMapper;
import com.zrwl.poscloud.xo.service.TimeTableDetailService;
import com.zrwl.poscloud.xo.vo.TimeTableDetailVO;

import javax.annotation.Resource;

/**
 * 管理员教师关联 服务实现类
 *
 * @author 刘思童 AutoGenerate
 * @date 2023-05-08
 */
@Service
public class TimeTableDetailServiceImpl extends SuperServiceImpl<TimeTableDetailMapper,TimeTableDetail> implements TimeTableDetailService {

    @Autowired
    private TimeTableDetailService timeTableDetailService;
    @Resource
    private TimeTableDetailMapper timeTableDetailMapper;

    @Override
    public IPage<TimeTableDetail> getPageList(TimeTableDetailVO timeTableDetailVO){
       QueryWrapper<TimeTableDetail> queryWrapper = new QueryWrapper<>();
       if (StringUtils.isNotEmpty(timeTableDetailVO.getKeyword()) && !StringUtils.isEmpty(timeTableDetailVO.getKeyword().trim())) {
            queryWrapper.like("name", timeTableDetailVO.getKeyword().trim());
       }
        if (StringUtils.isNotEmpty(timeTableDetailVO.getBatchUid()) && !StringUtils.isEmpty(timeTableDetailVO.getBatchUid().trim())) {
            queryWrapper.eq("batch_uid", timeTableDetailVO.getBatchUid());
        }
       Page<TimeTableDetail> page = new Page<>();
       page.setCurrent(timeTableDetailVO.getCurrentPage());
       page.setSize(timeTableDetailVO.getPageSize());
       queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
       queryWrapper.orderByDesc(SQLConf.CREATE_TIME);
       IPage<TimeTableDetail> pageList = timeTableDetailService.page(page, queryWrapper);
       List<TimeTableDetail> list = pageList.getRecords();
       pageList.setRecords(list);
       return pageList;
}
    @Override
    public List<TimeTableDetailVO> getAllList(TimeTableDetailVO timeTableDetailVO){
        List<TimeTableDetailVO> list = timeTableDetailMapper.getAllList(timeTableDetailVO);
        return list;
}
    @Override
    public String add(TimeTableDetailVO timeTableDetailVO){
        if(timeTableDetailVO.getDetailList().size() > 0){
            for (TimeTableDetailVO tableDetailVO : timeTableDetailVO.getDetailList()) {
                if(StringUtils.isNotBlank(tableDetailVO.getUid())){
                    if(tableDetailVO.getSubjectType() == 0){
                        delete(tableDetailVO);
                    }else {
                        tableDetailVO.setStatus(1);
                        edit(tableDetailVO);
                    }
                }else {
                    TimeTableDetail entity = new TimeTableDetail();
                    BeanUtils.copyProperties(tableDetailVO, entity, SysConf.STATUS);
                    entity.insert();
                    timeTableDetailVO.setUid(entity.getUid());
                }
            }
        }
        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);
    }

    @Override
    public String edit(TimeTableDetailVO timeTableDetailVO){
        TimeTableDetail entity = timeTableDetailService.getById(timeTableDetailVO.getUid());
        BeanUtils.copyProperties(timeTableDetailVO, entity);
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
}
    @Override
    public String delete(TimeTableDetailVO timeTableDetailVO){
        TimeTableDetail entity = timeTableDetailService.getById(timeTableDetailVO.getUid());
        entity.setStatus(EStatus.DISABLED);
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
}

    @Override
    public List<TimeTableDetailVO> getCurriculumDetailInfo(TimeTableDetailVO timeTableDetailVO) {
        return timeTableDetailMapper.getCurriculumDetailInfo(timeTableDetailVO);
    }

    @Override
    public List<TimeTableDetailVO> getTeacherTimeTableList(TimeTableDetailVO timeTableDetailVO) {
        List<TimeTableDetailVO> list = timeTableDetailMapper.getTeacherTimeTableList(timeTableDetailVO);
        return list;
    }
}
